//
//  Recommend_CollectionViewCell.swift
//  MyFood
//
//  Created by dllo on 16/2/16.
//  Copyright © 2016年 XRG. All rights reserved.
//

import UIKit

protocol Recommend_CollectionViewCellDelegate {

    func clickBanner(num: Int)
    func clickSearch()
    func clickFourButton(btnId:Int)
    func clickNewYearDIYBigTitlt(linkId: String)
    func clickNewYearImg(linkId: String)
    func clickMovie(moviePath: String)
    func clickGoodTitle()
    func clickGoodImage(linkId: String)
    func clickGoodUserHeadImg(linkId: String)
    func clickSecondTitle()
    func clickSecondImage(linkId: String)
    func clickSceneMoreClick()
    func clickImgTap(num: Int)

}

class Recommend_CollectionViewCell: UICollectionViewCell, ScrViewDelegate, UITableViewDelegate, UITableViewDataSource,R_First_TableViewCellDelegate, R_Third_NewYearThings_TVCellDelegate, R_Five_Good_TableViewCellDelegate, R_Second_Recommend_TableViewCellDelegate, R_Four_Scene_TableViewCellDelegate{

    var tableView: UITableView!
    var searchBarView : UIView!
    var delegate : Recommend_CollectionViewCellDelegate!
    let SCRVIEW_HEIGHT : CGFloat = 180
    var scrView : ScrView!

    var bannerArr : Array<F_LunBo_Model>?{
        didSet{
            self.scrView.bannerArr = bannerArr!
        }
    }

    var wigImageArr : Array<F_Wige_Image_Model>?{

        didSet{

            self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Automatic)
        }
    }

    var wigAnotherTwoArr : Array<F_Wige_Image_Model>?{

        didSet{

            self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Automatic)
        }
    }

    var todayRecommendArr : Array<F_Wige_Image_Model>?{

        didSet{

            self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 1)], withRowAnimation: UITableViewRowAnimation.Automatic)
        }
    }

    var newYearModel : F_widgetList_Model?{
        didSet{

            self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 2)], withRowAnimation: UITableViewRowAnimation.Automatic)
        }
    }

    var congeeModel : F_widgetList_Model?{
        didSet{

            self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 3)], withRowAnimation: UITableViewRowAnimation.Automatic)
        }
    }
    var chooseModel : F_widgetList_Model?{
        didSet{

            self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 5)], withRowAnimation: UITableViewRowAnimation.Automatic)
        }
    }







    override init(frame: CGRect) {
    super.init(frame: frame)

       createTableView()
       createSearchView()

    }
    func createTableView(){

        tableView = UITableView(frame: self.contentView.bounds, style: UITableViewStyle.Plain)
        self.contentView.addSubview(tableView)
        tableView.delegate = self
        tableView.dataSource = self

       tableView.registerNib(UINib(nibName: "R_First_TableViewCell", bundle: nil), forCellReuseIdentifier: "first")

        tableView.registerNib(UINib(nibName: "R_Second_Recommend_TableViewCell", bundle: nil), forCellReuseIdentifier: "second")

        tableView.registerNib(UINib(nibName: "R_Third_NewYearThings_TVCell", bundle: nil), forCellReuseIdentifier: "third")
        
        tableView.registerNib(UINib(nibName: "R_Four_Scene_TableViewCell", bundle: nil), forCellReuseIdentifier: "four")

        tableView.registerNib(UINib(nibName: "R_Five_Good_TableViewCell", bundle: nil), forCellReuseIdentifier: "five")

    }

    func  createSearchView(){

        scrView = ScrView(frame: CGRectMake(0, -SCRVIEW_HEIGHT - 50, BASE_WIDTH, SCRVIEW_HEIGHT))
        scrView.delegate = self
        scrView.backgroundColor = UIColor.greenColor()
        tableView.addSubview(scrView)

        searchBarView = UIView(frame: CGRect(x: 0, y: -50, width: self.frame.width, height: 50))
        tableView.addSubview(searchBarView)
        tableView.contentInset = UIEdgeInsetsMake(SCRVIEW_HEIGHT + 50, 0, 0, 0)

        let imageView = UIImageView(frame: searchBarView.bounds)
        imageView.image = UIImage(named: "搜索")
        imageView.userInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: "tapSearch")
        imageView.addGestureRecognizer(tap)
        searchBarView.addSubview(imageView)

    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 6
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 1
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        switch indexPath.section{

        case 1:
            return 200
        case 2:
            return 240
        case 3:
            return 240
        case 4:
            return 100
        case 5:
            return 230
            
        default:
            return 150
        }
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        switch indexPath.section{
        case 1:

            let secondCell = tableView.dequeueReusableCellWithIdentifier("second") as! R_Second_Recommend_TableViewCell
            secondCell.selectionStyle = UITableViewCellSelectionStyle.None

            if self.todayRecommendArr != nil {
                for model in self.todayRecommendArr!{
                    switch model.id {
                    case 1:
                    secondCell.leftImg.sd_setImageWithURL(NSURL(string: model.content))
                        break
                    case 3:
                        secondCell.leftLabelTitle.text = model.content
                        break

                    case 4:
                        secondCell.leftLabelDetail.text = model.content
                        break
                    case 5:
                        secondCell.midImg.sd_setImageWithURL(NSURL(string: model.content))
                        break

                    case 7:
                        secondCell.midLabelTitle.text = model.content
                        break
                    case 8:
                        secondCell.midLabelDetail.text = model.content
                        break
                    case 9:
                        secondCell.rightImg.sd_setImageWithURL(NSURL(string: model.content))
                        break
            
                    case 11:
                        secondCell.rightLabelTitle.text = model.content
                        break
                    case 12:
                        secondCell.rightLabelDetail.text = model.content
                        break

                    default:

                        break
                    }
                }

            }
            secondCell.delegate = self
            return secondCell

        case 2...3:
            
            let thirdCell = tableView.dequeueReusableCellWithIdentifier("third") as! R_Third_NewYearThings_TVCell
            thirdCell.selectionStyle = UITableViewCellSelectionStyle.None

            if indexPath.section == 2{
                thirdCell.num = 0
            if self.newYearModel != nil {

                thirdCell.bigTitleBtn.setTitle(self.newYearModel?.title, forState: UIControlState.Normal)
                thirdCell.descLabel.text = self.newYearModel!.desc
                
                for model in (self.newYearModel?.myWiget_Data)!{
                    switch model.id {
                    case 1:
                        thirdCell.leftBigImg.sd_setImageWithURL(NSURL(string: model.content))
                        break
                    case 2:
                thirdCell.titleLabel.text = model.content
                        break
                        
                   case 3:
                        thirdCell.titleSmallLabel.text = model.content
                        break
                    case 4:
                    thirdCell.midUpImg.sd_setImageWithURL(NSURL(string: model.content))
                        break
                    case 6:
                        thirdCell.midDownImg.sd_setImageWithURL(NSURL(string: model.content))
                        break

                    case 8:
                        thirdCell.rightUpImg.sd_setImageWithURL(NSURL(string: model.content))
                        break
                    case 10:
                        thirdCell.rightDownImg.sd_setImageWithURL(NSURL(string: model.content))
                        break

                    default:

                        break
                    }
                }
                            }

            }else{

                thirdCell.num = 1
                if self.congeeModel != nil {

                    thirdCell.bigTitleBtn.setTitle(self.congeeModel?.title, forState: UIControlState.Normal)
                    thirdCell.descLabel.text = self.congeeModel!.desc

                    for model in (self.congeeModel?.myWiget_Data)!{
                        switch model.id {
                        case 1:
                            thirdCell.leftBigImg.sd_setImageWithURL(NSURL(string: model.content))
                            break
                        case 2:
                            thirdCell.titleLabel.text = model.content
                            break

                        case 3:
                            thirdCell.titleSmallLabel.text = model.content
                            break
                        case 4:
                            thirdCell.midUpImg.sd_setImageWithURL(NSURL(string: model.content))
                            break
                        case 6:
                            thirdCell.midDownImg.sd_setImageWithURL(NSURL(string: model.content))
                            break

                        case 8:
                            thirdCell.rightUpImg.sd_setImageWithURL(NSURL(string: model.content))
                            break
                        case 10:
                            thirdCell.rightDownImg.sd_setImageWithURL(NSURL(string: model.content))
                            break
                            
                        default:
                            
                            break
                        }
                    }
                }

            }

            thirdCell.delegate = self
        return thirdCell

        case 4:

            let fourCell = tableView.dequeueReusableCellWithIdentifier("four") as! R_Four_Scene_TableViewCell
            fourCell.selectionStyle = UITableViewCellSelectionStyle.None
            fourCell.delegate = self
            return fourCell

        case 5:

            let fiveCell = tableView.dequeueReusableCellWithIdentifier("five") as! R_Five_Good_TableViewCell
            fiveCell.selectionStyle = UITableViewCellSelectionStyle.None

            if self.chooseModel != nil {

                fiveCell.titleBtn.setTitle(self.chooseModel?.title, forState: UIControlState.Normal)
                fiveCell.descLabel.text = self.chooseModel!.desc

                for model in (self.chooseModel?.myWiget_Data)!{
                    switch model.id {
                    case 1:
                        fiveCell.leftImg.sd_setImageWithURL(NSURL(string: model.content))
                        break
                    case 2:
                        fiveCell.leftHead.sd_setImageWithURL(NSURL(string: model.content))
                        break

                    case 3:
                        fiveCell.leftUserName.text = model.content
                        break
                    case 4:
                        fiveCell.midImg.sd_setImageWithURL(NSURL(string: model.content))
                        break
                    case 5:
                        print(model.content)
                        fiveCell.midHead.sd_setImageWithURL(NSURL(string: model.content))
                        break

                    case 6:
                        fiveCell.midUserName.text = model.content
                        break
                    case 7:
                        fiveCell.rightImg.sd_setImageWithURL(NSURL(string: model.content))
                        break
                    case 8:
                    fiveCell.rightHead.sd_setImageWithURL(NSURL(string: model.content))
                        break

                    case 9:
                        fiveCell.rightUserName.text = model.content
                        break

                    default:

                        break
                    }
                }
            }

            fiveCell.delegate = self
            return fiveCell



        default:

            let firstCell = tableView.dequeueReusableCellWithIdentifier("first") as! R_First_TableViewCell
            firstCell.selectionStyle = UITableViewCellSelectionStyle.None
            
        if self.wigImageArr != nil {
            for model in self.wigImageArr!{

            switch model.id {
            case 1:
            firstCell.new.sd_setBackgroundImageWithURL(NSURL(string: model.content), forState: UIControlState.Normal)
            
            break
            case 2:
            firstCell.newLabel.text = model.content
            case 3:
            firstCell.shicai.sd_setBackgroundImageWithURL(NSURL(string: model.content), forState: UIControlState.Normal)

                break
            case 4:
                firstCell.shicaiLabel.text = model.content
            case 5:
                firstCell.changjing.sd_setBackgroundImageWithURL(NSURL(string: model.content), forState: UIControlState.Normal)

                break
            case 6:
                firstCell.changjingLabel.text = model.content

            case 7:
                firstCell.love.sd_setBackgroundImageWithURL(NSURL(string: model.content), forState: UIControlState.Normal)

                break
            case 8:
                firstCell.loveLabel.text = model.content
            default:
                break
                }
            }
        }

            if self.wigAnotherTwoArr != nil{
                for model in self.wigAnotherTwoArr!{
                    switch model.id{
                    case 1:
                        firstCell.leftImg.sd_setImageWithURL(NSURL(string: model.content))
                    break
                    case 2:
                      firstCell.rightImg.sd_setImageWithURL(NSURL(string: model.content))
                        break
                    default:

                        break
                    }
                }
            }

            firstCell.delegate = self
            return firstCell
        }
    }


    func tapSearch(){

        self.delegate.clickSearch()
    }

    func click(num: Int) {

        self.delegate.clickBanner(num)

    }

    func clickFourButton(btnId: Int) {
        self.delegate.clickFourButton(btnId)
    }

    func clickImgTap(num: Int) {
        self.delegate.clickImgTap(num)
    }

    func clickSecondTitle() {
        self.delegate.clickSecondTitle()
    }

    func clickSecond_Movie(num: Int) {
        if self.todayRecommendArr != nil{
        for model in self.todayRecommendArr!{
            if model.id == num{
                // 今日食谱点击视频,传送movie Path,和DIY一样
                self.delegate.clickMovie(model.content)
            }
        }
        }
    }

    func clickSecondImage(num: Int) {

        if self.todayRecommendArr != nil {
        let model = self.todayRecommendArr![num - 1]
        print(model.link)
        let arr = model.link.componentsSeparatedByString("#")
        self.delegate.clickSecondImage(arr[1])
        }

    }

    func clickNewYearDIY(sectionNum: Int, num: Int) {
        if sectionNum == 0{
            if self.newYearModel?.myWiget_Data != nil{
        for model in (self.newYearModel?.myWiget_Data)!{
            if model.id == num{
                self.delegate.clickMovie(model.content)
            }
        }
            }
        }else{
            if self.congeeModel?.myWiget_Data != nil {
            for model in (self.congeeModel?.myWiget_Data)!{
                if model.id == num{
                self.delegate.clickMovie(model.content)
                }
            }
            }
        }
    }

    // 跳转到场景的页面. 暂时是一个web
    func clickNewYearDIYBigTitlt(num: Int) {

        if num == 0{
            if self.newYearModel != nil{
            let arr = self.newYearModel?.title_link.componentsSeparatedByString("#")
            self.delegate.clickNewYearDIYBigTitlt(arr![1])
            }
        }else{
            if self.congeeModel != nil {
            let arr = self.congeeModel?.title_link.componentsSeparatedByString("#")
            self.delegate.clickNewYearDIYBigTitlt(arr![1])
            }
        }

    }

    func clickNewYearDIYImg(section: Int, num: Int) {

        if section == 0{
            if self.newYearModel != nil{
            let model = self.newYearModel?.myWiget_Data[num - 1]
            let arr = model?.link.componentsSeparatedByString("#")
            self.delegate.clickNewYearImg(arr![1])
            }

        }else{
            if self.congeeModel != nil {
            let model = self.congeeModel?.myWiget_Data[num - 1]
            let arr = model?.link.componentsSeparatedByString("#")
            self.delegate.clickNewYearImg(arr![1])
            }
        }

    }
    


    func clickSceneMoreClick() {
        self.delegate.clickSceneMoreClick()
    }

    func clickGoodTitle() {
        self.delegate.clickGoodTitle()
    }

    func clickGoodImage(num: Int) {
        if self.chooseModel != nil{
        let model = self.chooseModel?.myWiget_Data[num - 1]
        let arr = model?.link.componentsSeparatedByString("#")
        self.delegate.clickGoodImage(arr![1])
        }

    }

    func clickGoodUserHeadImg(num: Int) {
        if self.chooseModel != nil {
        let model = self.chooseModel?.myWiget_Data[num - 1]
        let arr = model?.link.componentsSeparatedByString("#")
        self.delegate.clickGoodUserHeadImg(arr![1])
        }
    }






    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    override func awakeFromNib() {
        super.awakeFromNib()


        // Initialization code
    }

}
